package com.yanqu.road.dao.impl.activity.xiuxian;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.stronghold.StrongholdWarUserData;
import com.yanqu.road.entity.activity.xiuxian.data.XiuXianUserBehaviorData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class XiuXianUserBehaviorDataDaoImpl extends TempDataObjectDao<XiuXianUserBehaviorData> {

    public static String getXiuXianUserBehaviorDataSql = "select * from `t_u_activity_xiuxian_behavior_data` where `activityId`=? and `userId`=?";
    public List<XiuXianUserBehaviorData> getXiuXianUserBehaviorData(int activityId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<XiuXianUserBehaviorData> dataList = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getXiuXianUserBehaviorDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                XiuXianUserBehaviorData data = parse(rs);
                dataList.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return dataList;
    }

    public static String updateXiuXianUserBehaviorDataSql = "update `t_u_activity_xiuxian_behavior_data` set count=?  where activityId=? and userId=? and missionId=? and behaviorType=? and behaviorParam=?";
    @Override
    protected boolean updateData(XiuXianUserBehaviorData xiuXianUserBehaviorData) {
        xiuXianUserBehaviorData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, xiuXianUserBehaviorData.getCount()));
        para.put(i++, new DbParameter(Types.INTEGER, xiuXianUserBehaviorData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, xiuXianUserBehaviorData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, xiuXianUserBehaviorData.getMissionId()));
        para.put(i++, new DbParameter(Types.INTEGER, xiuXianUserBehaviorData.getBehaviorType()));
        para.put(i++, new DbParameter(Types.INTEGER, xiuXianUserBehaviorData.getBehaviorParam()));
        if (getDb().executeNoneQuery(updateXiuXianUserBehaviorDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertXiuXianUserBehaviorDataSql = "insert into `t_u_activity_xiuxian_behavior_data`(activityId,userId,missionId,behaviorType,behaviorParam,count) values (?,?,?,?,?,?)";
    @Override
    protected boolean addData(XiuXianUserBehaviorData xiuXianUserBehaviorData) {
        xiuXianUserBehaviorData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, xiuXianUserBehaviorData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, xiuXianUserBehaviorData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, xiuXianUserBehaviorData.getMissionId()));
        para.put(i++, new DbParameter(Types.INTEGER, xiuXianUserBehaviorData.getBehaviorType()));
        para.put(i++, new DbParameter(Types.INTEGER, xiuXianUserBehaviorData.getBehaviorParam()));
        para.put(i++, new DbParameter(Types.INTEGER, xiuXianUserBehaviorData.getCount()));
        if (getDb().executeNoneQuery(insertXiuXianUserBehaviorDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected XiuXianUserBehaviorData parseData(ResultSet rs) throws SQLException {
        XiuXianUserBehaviorData xiuXianUserBehaviorData = new XiuXianUserBehaviorData();
        xiuXianUserBehaviorData.setActivityId(rs.getInt("activityId"));
        xiuXianUserBehaviorData.setUserId(rs.getLong("userId"));
        xiuXianUserBehaviorData.setMissionId(rs.getInt("missionId"));
        xiuXianUserBehaviorData.setBehaviorType(rs.getInt("behaviorType"));
        xiuXianUserBehaviorData.setBehaviorParam(rs.getInt("behaviorParam"));
        xiuXianUserBehaviorData.setCount(rs.getInt("count"));

        return xiuXianUserBehaviorData;
    }
}
